package org.web2024.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.web2024.dao.Result;
import org.web2024.dao.Student;
import org.web2024.service.serviceimp.AdminService;
import org.web2024.utils.CaculateUtil;
import org.web2024.dao.Teachar;
import java.util.HashMap;
import java.util.Map;
import java.awt.font.MultipleMaster;
import java.util.List;
import org.web2024.dao.Course;
@RestController
@RequestMapping("/admin")
public class Admin {
    @Autowired
    private AdminService adminService;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @PostMapping("/coursefile")
    public Result coursefile(@RequestBody MultipartFile coursefile){
       return adminService.coursefile(coursefile);
    }
    @PostMapping("/studentfile")
    public Result studentfile(@RequestBody MultipartFile studentfile){
        return adminService.studentfile(studentfile);
    }


    @PostMapping("/teacherfile")
    public Result teacherfile(@RequestBody MultipartFile teacherfile){
        return adminService.teacherfile(teacherfile);
    }

    @PostMapping("/addterm")
    public Result addterm(@RequestParam String term){
        return adminService.addterm(term);
    }



    @PostMapping("/enablecourse")
    public Result enablecourse(@RequestParam String term,@RequestParam String starttime,@RequestParam String endtime){

        if(!CaculateUtil.isValidSemester(term)||!CaculateUtil.isValidDateTime(starttime)||!CaculateUtil.isValidDateTime(endtime)||!CaculateUtil.isValidDateTime(starttime))
            return Result.fail("格式不正确");
        if(redisTemplate.opsForHash().get("currentterm","term")!=null){
            return Result.fail("当前学期未结束");
        }
        return adminService.enableTerm(term,starttime,endtime);
    }

    @PostMapping("/showStuInfo")
    public Result showStuInfo(@RequestBody Student student){

        Student student1=(adminService.showStudentInfo(student));
        if(student1==null){
            return Result.fail("该学生不存在");
        }else return Result.success(student1);
    }
    @PostMapping("/admModifyStuInfo")
    public Result admModifyStuInfo(@RequestBody Student student){
        if(adminService.admModifyStuInfo(student)){
            return Result.success("修改成功");
        }else return Result.fail("修改失败");
    }
    @PostMapping("/modifyCourseInfo")
    public Result modifyCourseInfo(@RequestBody Course course){
        System.out.println("11111111");
        System.out.println(course);
        if(adminService.modifyCourseInfo(course)){
            return Result.success("修改成功");
        }else return Result.fail("修改失败");
    }

    @PostMapping("/showTeaInfo")
    public Result showTeaInfo(@RequestBody Teachar teacher){
        Teachar teacher1=(adminService.showTeaInfo(teacher));
        if(teacher1==null){
            return Result.fail("教师不存在");
        }else return Result.success(teacher1);
    }
    @PostMapping("/admModifyTeaInfo")
    public Result adModifyTeaInfo(@RequestBody Teachar teacher){

        System.out.println(teacher);
        if(adminService.admModifyTeaInfo(teacher)){
            return Result.success("修改成功");
        }else return Result.fail("修改失败");
    }
    @PostMapping("/stuCourseInfo")
    public Result stuCourseInfo(@RequestBody HashMap<String,String>data){
        List<Course> course=adminService.stuCourseInfo(data);
        if(course.isEmpty()){
            return Result.fail("该学生未选课或学号不存在或学期不存在");
        }else return Result.success(course);

    }
    @PostMapping("/teaCourseInfo")
    public Result teaCourseInfo(@RequestBody HashMap<String,String>data){
        List<Course> course=adminService.teaCourseInfo(data);
        if(course.isEmpty()){
            return Result.fail("该教师未发布课程或教师号不存在或学期不存在");
        }else return Result.success(course);

    }
    @PostMapping("/showCourseInfo")
    public Result showCourseInfo(@RequestBody Course course){
        Course course1 =adminService.showCourseInfo(course.getC_no());
        if(course1.getC_no() == null){
            return Result.fail("该课程不存在");
        }else return Result.success(course1);
    }
    @PostMapping("/showCourseStudents")
    public Result showCourseStudents(@RequestBody Course course){
        List<Student> students=adminService.showCourseStudents(course.getC_no());
        if(students.isEmpty()){
            return Result.fail("该课程没有学生选课");
        }else return Result.success(students);
    }
    @PostMapping("/deleteCourse")
    public Result deleteCourse(@RequestBody Course course){
        if(adminService.deleteCourse(course.getC_no())){
            return Result.success("删除成功");
        }else return Result.fail("删除失败");
    }
@PostMapping("/deleteStudent")
    public Result deleteStudent(@RequestBody Student student){
        if(adminService.deleteStudent(student.getS_no())){
            return Result.success("删除成功");
        }else return Result.fail("删除失败");
    }
}
